package com.aaa.mrcai.service.impl;

import com.aaa.mrcai.mapper.UserLoginMapper;
import com.aaa.mrcai.mapper.UserPermissionMapper;
import com.aaa.mrcai.pojo.entity.UserInfo;
import com.aaa.mrcai.pojo.entity.UserLogin;
import com.aaa.mrcai.pojo.entity.UserPermission;
import com.aaa.mrcai.result.AjaxResult;
import com.aaa.mrcai.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletContext;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Demo class
 *
 * @Author: Wang
 * @Date: Created in 10:10 2021/12/8
 */
@Service
public class UserLoginServiceImpl implements UserLoginService {

    @Autowired
    private UserLoginMapper userLoginMapper;

    @Autowired
    private UserPermissionMapper userPermissionMapper;


    @Autowired
    private ServletContext servletContext;

    @Override
    public AjaxResult userLogin(String telephone, String password) {
        Long telephoneExist = userLoginMapper.isTelephoneExist(telephone);
        if(telephoneExist == 0){
            return AjaxResult.defeated("不存在此手机号 请先注册");
        }
        UserLogin userLogin = userLoginMapper.userLogin(telephone, password);
        System.out.println(userLogin);
        if (userLogin==null){
            return AjaxResult.defeated("手机号或密码错误");
        }
        UserInfo user = userLoginMapper.getUserById(userLogin.getUid());
        servletContext.setAttribute("user",user);

        System.out.println("user+ddddddddd"+user);
        System.out.println("user:session"+user);
        Integer powerid = user.getUserLogin().getPowerid();
        if(powerid == 1){
           return new AjaxResult(4,"操作成功",null);
        }
        return AjaxResult.success(user);
    }

    @Override
    public AjaxResult getInfo() {
        UserInfo user = (UserInfo) servletContext.getAttribute("user");
        Map<String, Object> map = new HashMap<>();
        map.put("userInfo",user);
        Integer powerid = user.getUserLogin().getPowerid();
        System.out.println(powerid);

        Integer bid = user.getUserLogin().getBid();
        int i = userLoginMapper.businessState(bid);
        /*System.out.println("iiiiiii"+i);*/

        if (i == 1){
            List<UserPermission> userPers = userPermissionMapper.getUserPers();
            map.put("userPers",userPers);
        }else {
            List<UserPermission> businessPers = userPermissionMapper.getBusinessPers(powerid);
            map.put("userPers",businessPers);
        }
        System.out.println(map);
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult businessLogin() {
        UserInfo user = (UserInfo) servletContext.getAttribute("user");
        if (user == null){
            return AjaxResult.defeated("请先进行登录");
        }
        Integer powerid = user.getUserLogin().getPowerid();
        if(powerid == 0){
            return new AjaxResult(3,"操作成功",null);
        }
        return AjaxResult.success();
    }

    @Override
    public void logout() {
        //servletContext.invalidate();
        servletContext.removeAttribute("user");
    }

    @Override
    public AjaxResult getBid() {
        UserInfo user = (UserInfo) servletContext.getAttribute("user");
        Integer bid = user.getUserLogin().getBid();
        System.out.println(bid);
        return AjaxResult.success(bid);
    }

    @Override
    public AjaxResult getUid() {
        UserInfo user = (UserInfo) servletContext.getAttribute("user");
        Integer uid = user.getUserLogin().getUid();
        return AjaxResult.success(uid);
    }
}
